package com.example.demo.util;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/*
* 事务字段自动填充
* */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // 插入时填充字段
//        this.strictInsertFill(metaObject, "createdBy", String.class, "当前用户ID或用户名"); // 注意：这里"当前用户ID或用户名"需要根据实际业务逻辑获取
        metaObject.setValue("createdTime", null);
        this.strictInsertFill(metaObject, "createdTime", LocalDateTime.class, LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {

        // 更新时填充字段
//        this.strictUpdateFill(metaObject, "updatedBy", String.class, "当前用户ID或用户名"); // 注意：同上
        metaObject.setValue("updatedTime", null);
        this.strictUpdateFill(metaObject, "updatedTime", LocalDateTime.class, LocalDateTime.now());
//        metaObject.setValue("updatedTime",LocalDateTime.now());
    }
}
